import {
  request
} from "../../request/index.js";
var app = getApp();
var uploadImage = require('../../js/uploadImg/uploadImg.js');
var util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: "",
    imgUrl:[],
    min: 0,
    max: 200,
    radioCheck: false,
    index: 0,
    multiIndex: [0],
    phone: "",
    fileList:[],
    multiArray: [
      // ['请选择栏目分类', '废钢', '二手钢材', '二手钢结构', '二手设备', '废纸塑料', '有色金属', '一手户头', '行情报价', '求购信息']
      ['请选择栏目分类', '供应信息',  '求购信息']
    ],
    objectMultiArray: [{
        id: -1,
        name: '请选择栏目分类'
      },
      {
        id: 10,
        name: '供应信息'
      },
      {
        id: 3,
        name: '求购信息'
      },
      {
        id: 11,
        name: '废钢'
      },
      {
        id: 12,
        name: '二手钢材'
      },
      {
        id: 13,
        name: '二手钢结构'
      },
      {
        id: 14,
        name: '二手设备'
      },
      {
        id: 15,
        name: '废纸塑料'
      },
      {
        id: 16,
        name: '有色金属'
      },
      {
        id: 17,
        name: '一手户头'
      },
      {
        id: 2,
        name: '行情报价'
      }
    ],
    content: "",
    pics: [],
    h: false,
    type: "",
    picUrl: [],
    gg:false,
    picList:"",
    isOnShow:'1'
  },
  
  bindMultiPickerChange: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value,
      type: this.data.objectMultiArray[e.detail.value].id
    })
    
    if(this.data.type==2){
      this.setData({
        gg:true
      })
    }else{
      this.setData({
        gg:false
      })
    }
  },

  bindMultiPickerColumnChange: function (e) {
    // console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    // switch (e.detail.column) {
    //   case 0:
    //     switch (data.multiIndex[0]) {
    //       case 0:
    //         data.multiArray[1] = ['废钢', '二手钢材', '二手钢结构', '二手设备', '废纸塑料', '有色金属', '一手户头', '行情报价'];
    //         break;
    //         //   case 1:
    //         //     data.multiArray[1] = [''];
    //         //     break;
    //         //   case 2:
    //         //     data.multiArray[1] = [''];
    //         //     break;
    //     }
    //     break;
    // }
    this.setData(data);

    this.setData({
      type: this.data.objectMultiArray[e.detail.value].id
      //type: this.data.objectMultiArray[e.detail.column][e.detail.value].id
    })
    console.log(this.data.type)
  },
  //这个就是单选按钮的选中和取消选中事件
  isFalg() {
    var flag = this.data.radioCheck;
    var that = this
    if (this.data.type == 2) {
      this.setData({
        radioCheck: !flag
      })
      for (var i = 0; i < this.data.pics.length; i++) {
        // wx.uploadFile({
        //   //url: 'https://www.chengkoo.cn/MetalSales/addPic',
        //   url:'https://chengku.oss-cn-beijing.aliyuncs.com',
        //   filePath: this.data.pics[i],
        //   name: "file",
        //   success: function (res) {
        //     var list1 = that.data.picUrl;
        //     var list2 = res.data
        //     list1 = list1.concat(list2);
        //     that.setData({
        //       picUrl: list1
        //     })
        //   }
        // })
      }
    } else {
      if (this.data.pics.length == 0) {
        wx.showToast({
          title: '请先上传图片',
          icon: "none",
          duration: 1000
        })
        this.setData({
          radioCheck: flag
        })
      } else {
        this.setData({
          radioCheck: !flag
        })
        for (var i = 0; i < this.data.pics.length; i++) {
          // wx.uploadFile({
          //   //url: 'https://www.chengkoo.cn/MetalSales/addPic',
          //   url: 'https://chengku.oss-cn-beijing.aliyuncs.com',
          //   filePath: this.data.pics[i],
          //   name: "file",
          //   success: function (res) {
          //     var list1 = that.data.picUrl;
          //     var list2 = res.data
          //     list1 = list1.concat(list2);
          //     that.setData({
          //       picUrl: list1
          //     })
          //   }
          // })

        }
      }
    }

  },
  //上传图片
  getDrawimg() {
    var that = this
    var userId = app.globalData.userId;
    if (that.data.pics.length == 3) {
      wx.showToast({
        title: '最多上传三张图片',
        icon: "none"
      })
      return;
    }
    that.data.isOnShow='0'
    wx.chooseImage({
      count: 3,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
      
        var list1 = that.data.pics;

        //list2为oss的bucket地址
        var list2 = res.tempFilePaths;
        for(var i = 0; i < list2.length; i++){

          uploadImage(list2[i],'images/'+Date.parse(new Date())+'/',
          
          //result返回为url地址
          function(result){
            that.data.fileList.push(result)
              var picList = result;
           console.log("上传成功，地址为:",picList);
            
                  },
          function(result){
           console.log("上传失败:",result);
           
               })
          
        };
        list1 = list1.concat(list2);
        that.setData({
          pics: list1,
          h: false 
        })
      }
    })
    
  },
  //提交信息
  releaseSuccess() {
    var that = this;
    var userId = app.globalData.userId;
    var imgUrl = this.data.picUrl
    

    request({
      url: "/UserInfo/getUserInfo",
      data: {
        userId: userId
        
      }
    }).then(r => {
      console.log(r)
      if (r.data.isEnable == 2) {
        wx.showToast({
          title: "您的账号无法发布信息，请咨询客服",
          duration: 2000,
          icon: "none"
        }, 1000)
        return;
      }
      //判断是否选中已同意
      if (!this.data.radioCheck) {
        //未选中给个提示
        wx.showToast({
          title: "请勾选我已阅读并同意",
          duration: 2000,
          icon: "none"
        }, 1000)
        return;
      }
      if (this.data.type == -1 || !this.data.type) {
        wx.showToast({
          title: "请选择发布类别",
          duration: 2000,
          icon: "none"
        }, 1000)
        return;
      }
      let picList = ''
      for(let i=0; i<this.data.fileList.length; i++)
          picList += (i===0?'':',')+this.data.fileList[i]
      request({
        url: "/Resources/addResourceByUser",
        data: {
          resourcesDescribe: this.data.content,
          userId: userId,
          phone: this.data.phone,
          resourcesType: this.data.type,
          picUrl: this.data.imgUrl,
          picUrls:picList
        }

      }).then(res => {
        this.setData({
          content: "",
          phone: "",
          type: 11,
          pics: [],
          title: "",
          picUrl: [],
          radioCheck: false,
          h:false,
          fileList:[],
     
        })
        wx.showToast({
          title: '发布成功',
          icon: 'success',
          duration: 600,
          mask: true,
          success: function () { //这里是成功之后执行的代码
            setTimeout(function () {
              //要延时执行的代码
              //页面专挑方法
              wx.navigateTo({
                url: "/pages/myRelease/myRelease?userId=" + userId
              })
            }, 800) //延迟时间
          },
        })
      })

    })

  },
  inputs: function (e) {
    var value = e.detail.value;
    var len = parseInt(value.length);
    if (len > this.data.max) return;
    this.setData({
      currentWordNumber: len,
      content: value
    });
  },
  input: function (e) {
    var value = e.detail.value;
    this.setData({
      phone: value
    });
  },

  appNumber(e) {
    console.log(e);
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("onLoad发布页面刷新完成")
  
    if (options.isshare == 1) {
      wx.navigateTo({
        url: '../guang/guang'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log("onReady发布页面刷新完成")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("onShow发布页面刷新完成")
    if(this.data.isOnShow==='1'){
      this.setData({
        type: 11,
        pics: [],
        title: "",
        picUrl: [],
        radioCheck: false,
        h:false,
        fileList:[],
        isOnShow:'1'
      })
    }else{
      this.setData({
        isOnShow:'1'
      })
    }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/releaseContent/releaseContent?isshare=1',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})